package com.flymobi.mshop.activity;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.flymobi.mshop.R;

public class ShowImageActivity extends CommonActivity {
	
	private ImageView mImageView = null;

	private Bitmap m_bmpSource = null;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.show_image_activity);
//		MyApplication.getInstance().addActivity(this);

		mImageView = (ImageView)findViewById(R.id.imageView);
		mImageView.setOnClickListener(OnClickListener_ImageView);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null)
		{
			String strPath = extras.getString("path");
			
			loadImage(strPath);
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		if (m_bmpSource != null) {
			if (!m_bmpSource.isRecycled())
				m_bmpSource.recycle();
			m_bmpSource = null;						
		}
		System.gc();
	}  
	
	private OnClickListener OnClickListener_ImageView = new OnClickListener()
	{

		@Override
		public void onClick(View v) {
			finish();
		}
		
	};
	
	private void loadImage(String strPath)
	{
		
		int w = 480;
		int h = 800;
		
		
			
		if (strPath != null && strPath.length() > 0) {
			InputStream stream = null;
			BitmapFactory.Options opt = null;
			try {    
				FileInputStream inputstream = new FileInputStream(strPath);

				opt = new BitmapFactory.Options();
				opt.inJustDecodeBounds = true;
				BitmapFactory.decodeStream(inputstream, null, opt);

				int sideMax = w > h ? w : h;


				//    Boolean scaleByHeight = Math.abs(opt.outHeight - TARGET_HEIGHT) >= Math.abs(opt.outWidth - TARGET_WIDTH);
				Boolean scaleByHeight = Math.abs(opt.outHeight - sideMax) < Math.abs(opt.outWidth - sideMax);
				if(opt.outHeight * opt.outWidth * 2 >= w*h*2){
					double sampleSize = scaleByHeight ? opt.outHeight / sideMax : opt.outWidth / sideMax;
					opt.inSampleSize = (int)Math.pow(2d, Math.floor(Math.log(sampleSize) / Math.log(2d)));
				}

				// Do the actual decoding
				inputstream.close();

				opt.inJustDecodeBounds = false;    
				inputstream = new FileInputStream(strPath);
				stream = new BufferedInputStream(inputstream);
				m_bmpSource = BitmapFactory.decodeStream(stream, null, opt);
				inputstream.close();

				if (m_bmpSource != null) {
					mImageView.setImageBitmap(m_bmpSource);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
			finally {
				if (stream != null) {
					try {
						stream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

				opt = null;
			}
		}
		
	}
}